﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using Kitware.VTK;


namespace HAVTKPack
{
    public  class ShowPlaneBarrierFilter:GeometryFilter
    {
        #region 构造函数

        public ShowPlaneBarrierFilter(string id,System .Drawing .Color lineColor,int solidWidth)
            : base(id)
        {

            this._lineColor = lineColor;

            this._solidWidth = solidWidth;


        }

        #endregion


        #region 字段属性



        System.Drawing.Color _lineColor = System.Drawing.Color.Red;

        public System.Drawing.Color LineColor
        {
            get { return _lineColor; }
            set {
                
                
                _lineColor = value;

                this.OnSetLineColorChanged();

            }
        }


        int _solidWidth = 1;

        public int SolidWidth
        {
            get { return _solidWidth; }
            set { 
                
                _solidWidth = value;

                this.OnSetSolidWidthChanged();
            }
        } 




        #endregion

        #region vtk流水线的对象



        vtkUnstructuredGrid _unStructuredGridData;

        vtkConnectivityFilter _con;

        vtkGeometryFilter _geo;

        vtkPolyData _polyData;


        vtkDataSetMapper _setMapper;

        vtkActor _actor;





        #endregion


        public override void Render()
        {
            try
            {

                _unStructuredGridData = (vtkUnstructuredGrid)this.ParentNode.Geometry.OutPutData;


                if (this._unStructuredGridData == null)
                {
                    return;
                }
                _con = vtkConnectivityFilter.New();
                _con.SetInput(_unStructuredGridData);

                _geo = vtkGeometryFilter.New();
                _geo.SetInput(_con.GetOutput());

                _polyData = vtkPolyData.New();
                _polyData = _geo.GetOutput();



                this.OutputPolyData = _geo.GetOutput();
                this.OutputUnStructuredGridData = _unStructuredGridData;




                _setMapper = vtkDataSetMapper.New();

                _setMapper.SetInput(_unStructuredGridData);

                _setMapper.ScalarVisibilityOff();
                


                //vtkDataSetMapper.SetResolveCoincidentTopologyToPolygonOffset();
                //vtkDataSetMapper.SetResolveCoincidentTopologyToShiftZBuffer();
              


                _actor = vtkActor.New();

               
                _actor.SetMapper(_setMapper);

                if (this.LineColor !=null )
                {
                    _actor.GetProperty().SetColor(LineColor.R / 255.0, LineColor.G / 255.0, LineColor.B / 255.0);//黑色
                }
                else
                {
                    _actor.GetProperty().SetColor(1, 0, 0);
                }


                _actor.GetProperty().EdgeVisibilityOn();
                //_actor.GetProperty().EdgeVisibilityOff ();
                //_actor.GetProperty().SetLineStippleRepeatFactor(1);
                _actor.GetProperty().SetLineWidth(this.SolidWidth );


                #region 尝试修改演员灯光设置

                //_actor.GetProperty().SetInterpolationToGouraud();
                //_actor.GetProperty().SetInterpolationToFlat ();
                //_actor.GetProperty().SetFrontfaceCulling(0);

                //_actor.GetProperty().BackfaceCullingOff();//cull剔除
                //_actor.GetProperty().BackfaceCullingOn();// 将不渲染看不见的隐消面  角色背面被遮挡部分不进行渲染  如果是一个面 当 灯光不能反射到眼睛（相机）时（灯光相关??也可能仅仅和面的正面向量有关??还是和点的绕序有关？？）看不到
                //_actor.GetProperty().FrontfaceCullingOff();
                //_actor.GetProperty().FrontfaceCullingOn();// 将不渲染看不见的隐消面  角色前面部分不进行渲染    如果是一个面 当 灯光能反射到眼睛（相机）时（灯光相关??也可能仅仅和面的正面向量有关??还是和点的绕序有关？？）看不到
                _actor.GetProperty().LightingOff();
                //_actor.GetProperty().LightingOn();
                //_actor.GetProperty().SetOpacity(1);


                //_actor.GetProperty().ShadingOff();//Enable/Disable shading. When shading is enabled, the Material must be set. 
                //vectorActor.GetProperty().ShadingOn();
        
                #endregion


                if (this._visiability)
                {
                    _actor.VisibilityOn();
                }
                else
                {
                    _actor.VisibilityOff();
                }




                _actor.GetProperty().SetRepresentationToSurface();

                //if (this._repType == REPRESENTATIONTYPE.POINT)
                //{
                //    _actor.GetProperty().SetRepresentationToPoints();
                //}
                //else if (this._repType == REPRESENTATIONTYPE.SURFACE)
                //{
                //_actor.GetProperty().SetRepresentationToSurface();
                //}
                //else if (this._repType == REPRESENTATIONTYPE.WIREFRAME)
                //{
                //    _actor.GetProperty().SetRepresentationToWireframe();
                //}




                this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().AddActor(_actor);






            }
            catch 
            {


                System.Windows.Forms.MessageBox.Show("ShowPlaneBarrierFilter渲染过程出错！");
                return;
            }


            //throw new NotImplementedException();
        }


        public override void StopRender()
        {
          
    
            if (this._actor != null)
            {
                this.ParentNode.Scene.VtkControl.RenderWindow.GetRenderers().GetFirstRenderer().RemoveActor(_actor);
                this._actor.Dispose();
            }


            if (this._setMapper != null)
            {
                this._setMapper.Dispose();
            }

            if (this._polyData != null)
            {
                this._polyData.Dispose();
            }
            if (this._geo != null)
            {
                this._geo.Dispose();
            }
            if (this._con != null)
            {
                this._con.Dispose();
            }
            if (this._unStructuredGridData != null)
            {
                this._unStructuredGridData.Dispose();
            }
          

            if (this.ParentNode.Geometry.OutPutData != null)
            {
                this.ParentNode.Geometry.OutPutData.Dispose();
            }


            //throw new NotImplementedException();
        }




        #region 属性改变方法


        private void OnSetLineColorChanged()
        {
            this.UpdateLineColor();

        }
        private void OnSetSolidWidthChanged()
        {

            this.UpdateSolidWidth();
        }

        #endregion


   


        #region 共有方法


        public void UpdateBarrierSetup(bool visible, int size, System.Drawing.Color color)
        {
            this.Visiability = visible;
            this._solidWidth = size;
            this._lineColor = color;



            this.UpdateSetup();
        }




        #endregion


        #region 内部方法


        private void UpdateSetup()
        {
            this.UpdateLineColor();
            this.UpdateSolidWidth();

        }

        private void UpdateLineColor()
        {

            if (this._actor != null)
            {
                if (this.LineColor != null)
                {
                    _actor.GetProperty().SetColor(LineColor.R / 255.0, LineColor.G / 255.0, LineColor.B / 255.0);//黑色
                }
                else
                {
                    _actor.GetProperty().SetColor(1, 0, 0);
                }
            }
        
        }
        private void UpdateSolidWidth()
        {

            if (this._actor != null)
            {
                _actor.GetProperty().SetLineWidth(this.SolidWidth);
            }

           
        
        }




        #endregion



        #region 重写父类属性方法

        protected override void OnSetVisibility()
        {
            base.OnSetVisibility();

            if (this.ParentNode.Scene == null)
            {
                return;

            }

            if (this.ParentNode.Scene.IsRendering)
            {
                if (this._visiability)
                {
                    this._actor.VisibilityOn();
                
                }
                else
                {
                    this._actor.VisibilityOff();
               
                }
             
            }


        }

        protected override void OnSetContourType()
        {
            base.OnSetContourType();


        }

        protected override void OnSetRepresentationType()
        {
            base.OnSetRepresentationType();


        }


      
        #endregion





    }


}
